<?php
/**
 * Copyright (C) 2009  Honghao Geng (genghonghao@gmail.com)
 * $Id: read.php 105 2009-07-31 17:40:33Z genghonghao $
 */
!defined('SMARTGOON_MAIN_LOADED') ? define('SMARTGOON_MAIN_LOADED', 1) : NULL;
require_once './config.php';
require_once './includes/common.php';

if (!isset($_GET['actid']))
{
	redirect_page('�Ҳ�������','����ID��������','./index.php');
}
else
{
	$article_id = intval(trim($_GET['actid']));
}
$images_smilies = get_smilies();
assign_right_list();
// �������
$a = mt_rand(1,20);
$b = mt_rand(1,7);
$c = $a+$b;

// ����Աɾ������
if(isset($_REQUEST['act']) && trim($_REQUEST['act']) == 'del' && isset($_REQUEST['repid']) && TRUE === $_SESSION['user']['islogin'])
{
	$db->query("DELETE FROM " . TB_REPLY . " WHERE reply_id='{$_REQUEST['repid']}'");
	if ($db->affected_rows() > 0)
	{
		// ��������1
		$db->query("UPDATE ". TB_ARTICLE . " SET repl_number=repl_number-1 WHERE article_id='{$article_id}'");
		redirect_page('�ɹ�','�ɹ�ɾ��һ������',$root_url.'/read.php?actid=' . $article_id);
	}
	else 
	{
		redirect_page('ʧ��','����ɾ��ʧ��',$root_url.'/read.php?actid=' . $article_id);
	}
}
// �������
if (isset($_POST['rname']))
{
	$rname  = isset($_POST['rname']) ? nl2br(trim($_POST['rname'])) : NULL;
	$remail = str_replace('������Email��ַ', '', $_POST['remail']);
	$remail = isset($_POST['remail']) ? nl2br(trim($remail)) : NULL;
	$rcontent = isset($_POST['rcontent']) ? nl2br(trim($_POST['rcontent'])) : NULL;
	$avatars = isset($_POST['avatars']) ? nl2br(trim($_POST['avatars'])) : NULL;
	$rtotal = isset($_POST['rtotal']) ? intval($_POST['rtotal']) : 0;
	$rcount = isset($_POST['rcount']) ? intval($_POST['rcount']) : 0;
	
	//$rcontent = htmlspecialchars($rcontent);
	
	foreach($images_smilies[1] as $k=>$v) {
		if(strpos($rcontent, $v) !== false)
		{
			$replace = '<img src=' . $root_url . '/images/smilies/' . substr($v, 9, -1) . '.gif border=0 />';
			$rcontent = str_replace($v, $replace, $rcontent);
		}
	}

	$time = time();
	$formt_time = date("Y-m-d H:i:s", $time);
	if ($rtotal != $rcount)
	{
		redirect_page('����ʧ��','��֤���ּ���ʧ��',$root_url.'/read.php?actid=' . $article_id);
	}
	if ( NULL === $rname || NULL === $rcontent || mb_strlen($rcontent) > 63000)
	{
		redirect_page('����ʧ��','�û���ͻظ����ݶ�����Ϊ��',$root_url.'/read.php?actid=' . $article_id);
	}
	
	$db->query("INSERT INTO " . TB_REPLY . " VALUES" .
			"(NULL, '{$article_id}', '{$rname}', '{$remail}', '{$client_ip}', '{$time}', '{$formt_time}', '{$rcontent}', '{$avatars}')");
	if ($db->affected_rows() > 0)
	{
		// ��������1
		$db->query("UPDATE ". TB_ARTICLE . " SET repl_number=repl_number+1 WHERE article_id='{$article_id}'");
		redirect_page('���۳ɹ�','лл�������',$root_url.'/read.php?actid=' . $article_id);
	}
	else 
	{
		redirect_page('����ʧ��','�����²�������',$root_url.'/read.php?actid=' . $article_id);
	}
	
}

$result = $db->result("SELECT * FROM " . TB_ARTICLE . " WHERE article_id='{$article_id}'");
$reply = $db->result("SELECT * FROM " . TB_REPLY . " WHERE article_id='{$article_id}' ORDER BY reply_id ASC");

$tpl->assign('article', $result[0]);
$tpl->assign('reply', $reply);
$tpl->assign('blog_keywords', $result[0]['keywords']);
$tpl->assign('blog_title', $result[0]['subject']);
require_once './header.php';

if (count($result) != 1)
{
	redirect_page('�Ҳ�������','����ID��������',$root_url.'/index.php');
}
if (isset($_SESSION['user']['username']))
{
	$tpl->assign('loginname', $_SESSION['user']['username']);
}

// �ж��Ƿ�Ϊ����Ա
if ($_SESSION['user']['islogin'] == TRUE) {
	$tpl->assign('is_admin', 1);
}
// ����Ķt���
if (!isset($_SESSION['user'][$article_id]))
{
	$db->query("UPDATE ". TB_ARTICLE . " SET click_number=click_number+1 WHERE article_id='{$article_id}'");
	$_SESSION['user'][$article_id] = TRUE;
}
$tpl->assign('smilies', $images_smilies[0]);
$avatars = get_avatars();
sort($avatars);
$tpl->assign('avatars', $avatars);

$tpl->assign('val1', $a);
$tpl->assign('val2', $b);
$tpl->assign('total', $c);
$tpl->display('read.html');
require_once './footer.php';

// ��ȡ����ͼ��
function get_smilies()
{
	global $root_url;
	$dir = 'images/smilies';
	$arr = array();
	$arr1 = array();
	if (is_dir($dir)) 
	{
    	if ($dh = opendir($dir))
		{
        	while (($file = readdir($dh)) !== false) 
        	{
        		if($file != '.' && $file != '..' && strpos($file, '.gif') !== false)
            	$arr[] = '<li style="float: left;"><img src=' . $root_url . '/images/smilies/' . $file . ' border=0 height=25 width=25/></li>';
            	$arr1[] = "[img src=" . substr($file, 0, strpos($file,'.'))."]";
        	}
        	closedir($dh);
    	}
	}
	return array($arr, $arr1);
}

// ��ȡͷ��
// ��ȡ����ͼ��
function get_avatars()
{
	global $root_url;
	$dir = 'images/avatars';
	$arr = array();
	if (is_dir($dir)) 
	{
    	if ($dh = opendir($dir))
		{
        	while (($file = readdir($dh)) !== false) 
        	{
        		if($file != '.' && $file != '..' && strpos($file, '.gif') !== false)
        		{
        			$arr[] = '<img src=' . $root_url . '/images/avatars/' . $file . ' border=0 height=35 width=35/>';
        		}
            	
        	}
        	closedir($dh);
    	}
	}
	return $arr;
}

?>
